#include "gamecore.h"
#include <QtMath>
#include <QTime>
#include <QDir>
#include <set>
#include <string>
#include <QDebug>
#include <QMessageBox>

GameCore::GameCore(QObject *parent) : QObject(parent),
    st("gameData.ini",QSettings::IniFormat)
{
    qsrand(QTime::currentTime().msecsSinceStartOfDay());
    initSettings();
}


void GameCore::initSettings(){
    readWordsVec();
    question.fileName=st.value("question/fileName").toString();
    question.GuessNum=st.value("question/guessNum").toInt();
    question.haveNoGuess=st.value("question/haveNoGuess").toStringList();
    question.showUiWords=st.value("question/showUiWords").toString();
    if(question.haveNoGuess.size()==0){
        firstRunInit();
    }
}

void GameCore::saveSettings(){
    st.setValue("question/fileName",question.fileName);
    st.setValue("question/guessNum",question.GuessNum);
    st.setValue("question/haveNoGuess",QVariant::fromValue(question.haveNoGuess));
    st.setValue("question/showUiWords",question.showUiWords);
}

void GameCore::firstRunInit(){
    question.haveNoGuess=wordsList;
    question.GuessNum=0;
    question.fileName.clear();
    question.showUiWords.clear();
    makeQuestion();
}


void GameCore::readWordsVec(){
    wordsList.clear();
    QDir dir(":/data");
    QFileInfoList infoList=dir.entryInfoList(QDir::Files);
    for(int i=0;i<infoList.count();++i){
        QFileInfo info=infoList.at(i);
        wordsList.push_back(info.baseName());
    }
}

void GameCore::makeQuestion(){
    int num=qrand()%question.haveNoGuess.size();
    question.fileName=question.haveNoGuess.at(num);
    QString likeWords=findAllLikeWords(question.fileName);
    if(likeWords.length()<12){
        int shouldAddWords=(12-likeWords.length()+3)/4;
        for(int i=0;i<shouldAddWords;++i){
            int t=qrand()%wordsList.count();
            likeWords+=wordsList.at(t);
        }
    }
    likeWords=likeWords.left(12);
    for(int i=0;i<12;++i){
        int t=qrand()%12;
        QChar c=likeWords[i];
        likeWords[i]=likeWords[t];
        likeWords[t]=c;
    }
    for(auto p:question.fileName){
        int t=qrand()%likeWords.length();
        likeWords.insert(t,p);
    }
    question.showUiWords=likeWords;
}

bool GameCore::checkAnswer(QString ans){
    if(ans==question.fileName){
        ++question.GuessNum;
        question.haveNoGuess.removeOne(question.fileName);
        if(question.haveNoGuess.size()==0){
            QMessageBox::information(0,"恭喜"," 狂拽酷炫吊炸天的你已完成题库中所有问题， 将重新开始游戏");
            firstRunInit();
        }else{
            makeQuestion();
        }
        return true;
    }else{
        return false;
    }
}

QString GameCore::findAllLikeWords(QString word){
    QString ret;
    for(auto t:word){
        for(auto p:wordsList){
            if(p.indexOf(t)!=-1&&p!=word){
                ret+=p;
            }
        }
    }
    std::set<QChar> tmp(ret.begin(),ret.end());
    ret.clear();
    for(auto p:tmp){
        ret.append(p);
    }
    for(auto p:word){
        ret.remove(p);
    }
    return ret;
}

GameCore::Question GameCore::getQuestion(){
    return question;
}
